﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using PPI.Client;
using PPI.Client.Interface;
using PPI.Client.Logic;
using PPI.Client.Logic.Inspection;
using PPI.Client.Presenter;
using PPI.Client.Utils;
using PPI.Entities;
using PPI.Entities.Inspections;
using PPI.Entities.Lookup;
using VMind.Global;

namespace PPI
{
    public partial class frmDeployInspectionDatabase : frmDeployBase
    {
        private List<ECompany> m_Companies;
        private List<EInspector> m_Inspectors;
        private List<EHandheld> m_Handhelds;
        private List<EVendor> m_ListVendors;
        private List<EVendor_Project> m_ListProjects;
        private List<EVendor_Project> m_ListSelectedProjects;
        private List<EVendor_Contact> m_ListSelectedContacts;


        public frmDeployInspectionDatabase()
        {
            InitializeComponent();
            base.MenuMain.Visible = false;
            this.FormSize = FormSizes.Medium;
            try
            {
                LoadCombos();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                RegisterError.SaveError(ex);
            }
        }

        private void LoadCombos()
        {
            m_ListVendors = LCacheDataSingleton.Instance.Vendor;
            m_Companies = LCacheDataSingleton.Instance.Companies;
            m_Handhelds = LCacheDataSingleton.Instance.Handhelds;
            DropdownHelper.FillDropDown(ddlCompany, m_Companies, typeof(ECompany), "Name", "Id", "Select an option...");
            DropdownHelper.FillDropDown(ddlHandheld, m_Handhelds, typeof(EHandheld), "Name", "Id", "Select an option...");
            DropdownHelper.FillDropDown(ddlVendor, m_ListVendors, typeof(EVendor), "Name", "VendorId", "Select an option...");
            addingHandheld1.CurrentHandhelds = m_Handhelds;


        }

        private void restartComboIndex()
        {
            DropdownHelper.ResetComboIndex(ddlHandheld);
            DropdownHelper.ResetComboIndex(ddlCompany);    
            DropdownHelper.ResetComboIndex(ddlInspector);
            DropdownHelper.ResetComboIndex(ddlVendor);
            DropdownHelper.ResetComboIndex(ddlProject);
            DropdownHelper.ResetComboIndex(ddlUser);
        }

        #region Control Events

        private void ddlHandheld_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }
        
        private void addingHandheld1_FinishInsert(object sender, EventArgs e)
        {
            ddlHandheld.Items.Clear();
            DropdownHelper.FillDropDown(ddlHandheld, addingHandheld1.CurrentHandhelds, typeof(EHandheld), "Name", "Id", "Select an option...");
            this.addingHandheld1.Hide();

        }

        private void btnAddHandheld_Click(object sender, EventArgs e)
        {
            this.addingHandheld1.Show();
        }

        private void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlInspector.Enabled = (ddlCompany.SelectedIndex > 0);
            int _selectedValue = 0;

            if (ddlCompany.SelectedIndex > 0)
            {
                ListBoxItem _selectedItem = (ListBoxItem)ddlCompany.SelectedItem;

                _selectedValue = ConvertHelper.ToInt32(_selectedItem.Value);

                m_Inspectors = m_Companies.Find(delegate(ECompany c) { return c.Id == _selectedValue; }).Inspectors;
                DropdownHelper.FillDropDown(ddlInspector, m_Inspectors, typeof(EInspector), "Name", "Id", "Select an option...");
            }
            ActivateDeploy();
        }

        private void ddlInspector_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }

        #endregion

        protected override void DoNewDeploy()
        {
            if (MessageBox.Show("Would you like to do another 'inspection' deployment?", "New Deploy", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                this.Close();
            else
                restartComboIndex();

        }

        protected override void DoDeploy()
        {
            try
            {
                if (ValidateAll())
                {
                    ListBoxItem _selectedItem = (ListBoxItem)ddlHandheld.SelectedItem;
                    EHandheld _Handheld = new EHandheld(ConvertHelper.ToInt32(_selectedItem.Value), ddlHandheld.Text);
                    _selectedItem = (ListBoxItem)ddlCompany.SelectedItem;
                    ECompany _Company = new ECompany(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);
                    _selectedItem = (ListBoxItem)ddlInspector.SelectedItem;
                    EInspector _Inspector = new EInspector(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);
                    _selectedItem = (ListBoxItem)ddlProject.SelectedItem;
                    EVendor_Project _Project = new EVendor_Project(ConvertHelper.ToInt32(_selectedItem.Value), ddlProject.Text);
                    _selectedItem = (ListBoxItem)ddlUser.SelectedItem;
                    EVendor_Contact _Contact = new EVendor_Contact(ConvertHelper.ToInt32(_selectedItem.Value), ddlUser.Text);
                    _selectedItem = (ListBoxItem)ddlVendor.SelectedItem;
                    EVendor _Vendor = new EVendor(ConvertHelper.ToInt32(_selectedItem.Value), ddlVendor.Text, "");

                    string sourceDatabase;
                    try
                    {
                        DeployInspectionDatabasePresenter logicDatabase =(DeployInspectionDatabasePresenter) m_Presenter;
                        sourceDatabase = logicDatabase.DeployInspection(_Company, _Inspector, _Handheld,_Project,_Contact,_Vendor);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                    FileInfo _FileToCopy;
                    DirectoryInfo _DestinationFolder = SelectDestinationFolder();
                    if (_DestinationFolder == null)
                        return;
                    m_Presenter.DownloadFile(sourceDatabase, _DestinationFolder.FullName + "\\" + Constants.K_LOCALDATABASEINSPECTIONS);
                    m_Presenter.CopyLocalFilesToDestination(_DestinationFolder.FullName);

                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void frmDeployInspectionDatabase_FormClosing(object sender, FormClosingEventArgs e)
        {
            restartComboIndex();
        }

        private void frmDeployInspectionDatabase_Load(object sender, EventArgs e)
        {
            try
            {
                m_Presenter = new DeployInspectionDatabasePresenter(this);
            }
            catch(Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProject.Enabled = (ddlVendor.SelectedIndex > 0);
            ddlUser.Enabled = (ddlVendor.SelectedIndex > 0);

            int _selectedValue = 0;

            //Load de ProjectCombo
            if (ddlVendor.SelectedIndex > 0)
            {
                ListBoxItem _selectedItem = (ListBoxItem)ddlVendor.SelectedItem;

                _selectedValue = ConvertHelper.ToInt32(_selectedItem.Value);

                m_ListSelectedProjects = m_ListVendors.Find(delegate(EVendor v) { return v.Id == _selectedValue; }).Projects;
                DropdownHelper.FillDropDown(ddlProject, m_ListSelectedProjects, typeof(EVendor_Project), "Name", "Vendor_ProjectId", "Select an option...");

                m_ListSelectedContacts = m_ListVendors.Find(delegate(EVendor v) { return v.Id == _selectedValue; }).Contacts;
                DropdownHelper.FillDropDown(ddlUser, m_ListSelectedContacts, typeof(EVendor_Contact), "Name", "Vendor_ContactId", "Select an option...");
            }
            ActivateDeploy();
        }

        private void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }

        private void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }

        

        

        

    }
}
